package com.kmust.dream.wanderingdream.donation.controller;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.kmust.dream.wanderingdream.comm.pojo.User;
import com.kmust.dream.wanderingdream.donation.service.*;
import com.kmust.dream.wanderingdream.donation.utils.JwtUtils;
import com.kmust.dream.wanderingdream.donation.vo.BasicInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.kmust.dream.wanderingdream.donation.entity.BasicInformationDonationEntity;
import com.kmust.dream.wanderingdream.comm.utils.PageUtils;
import com.kmust.dream.wanderingdream.comm.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author Leng
 * @email Leng81605@gmail.com
 * @date 2021-07-12 21:35:52
 */
@RestController
@Api(tags = "受捐赠者基本信息")
@RequestMapping("donation/base")
public class BasicInformationDonationController {
    @Autowired
    private BasicInformationDonationService basicInformationDonationService;
    @Autowired
    private OthersInformationDonationService othersInformationDonationService;
    @Autowired
    private RecipientUserUnionService recipientUserUnionService;
    @Autowired
    private AwardInformationDonationService awardInformationDonationService;
    @Autowired
    private LearningstatusInformationDonationService learningstatusInformationDonationService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取带有分页信息的所有记录")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = basicInformationDonationService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{recipientId}")
    @ApiOperation(value = "获取指定recipientId的记录")
    public R info(@PathVariable("recipientId") String recipientId){
		BasicInformationDonationEntity basicInformationDonation = basicInformationDonationService.getById(recipientId);

        return R.ok().put("basicInformationDonation", basicInformationDonation);
    }

    @GetMapping("/info/getUserById/{recipientId}")
    @ApiOperation(value = "获取指定recipientId的用户信息")
    public User getUserById(@PathVariable("recipientId") String recipientId){
        User user = basicInformationDonationService.getUserByReciId(recipientId);
        return user;

    }


    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存请求捐赠记录")
    public R save(@Validated BasicInfoVo basicInfoVo, BindingResult result, HttpServletRequest request) throws Exception {
        if (result.hasErrors()){
           String msg = result.getFieldError().getDefaultMessage();
           return R.ok(msg);
        }
        String username = JwtUtils.getUserUNByJwtToken(request);
        System.out.println(basicInfoVo);
        if (username.equals("")){
            return R.ok("当前登录用户不能为空,请先登录!");
        }
		basicInformationDonationService.saveBasicInfo(basicInfoVo,username);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "更改记录信息")
    public R update(BasicInformationDonationEntity basicInformationDonation){
        System.out.println(basicInformationDonation);
		basicInformationDonationService.updateById(basicInformationDonation);

        return R.ok();
    }
    @GetMapping("/getallstudent")
    public List<BasicInformationDonationEntity> getall(){

        return basicInformationDonationService.getStudentList();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除指定recipientId记录")
    public R delete(@RequestBody String[] recipientIds){
		basicInformationDonationService.removeByIds(Arrays.asList(recipientIds));

        return R.ok();
    }

    @GetMapping("/getListByUserName/{username}")
    public List<BasicInformationDonationEntity> getListByUserName(@PathVariable("username") String username){
       List<BasicInformationDonationEntity> list = basicInformationDonationService.getListByUserName(username)
               .stream().map(s->s.setText(
                       othersInformationDonationService.getTextByRId(s.getRecipientId())
               )).collect(Collectors.toList());
       return list;
    }

    @GetMapping("/getListByType/{school}/{curr}")
    public List<BasicInformationDonationEntity> getListByType(@PathVariable("school") String school,@PathVariable("curr") int curr){

        List<BasicInformationDonationEntity> list =basicInformationDonationService.getListByType(school,curr);
        return list;

    }
    @GetMapping("/getPageSize/{type}")
    public int getSize(@PathVariable("type") String type){
        return basicInformationDonationService.getPageSize(type);
    }

    @GetMapping("/getBasicByUserName/{username}")
    public List<BasicInformationDonationEntity> getBasicList(@PathVariable("username") String username){
        return basicInformationDonationService.getBasicByUserName(username);
    }
}
